package com.framework.bdf4j.socket.nclient;

import java.io.IOException;

import com.framework.bdf4j.socket.coder.LengthFieldBasedFrameDecoder;
import com.framework.bdf4j.socket.coder.LengthFieldPrepender;
import com.framework.bdf4j.socket.coder.MyEncoder;
import com.framework.bdf4j.socket.coder.StringDecoder;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
* Netty客户端
**/

public class NettyClientUtil {
 
  public static String helloNetty(String msg) {
    NettyClientHandler nettyClientHandler = new NettyClientHandler();
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        //该参数的作用就是禁止使用Nagle算法，使用于小数据即时传输
        .option(ChannelOption.TCP_NODELAY, true)
        .channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>() {
          @Override
          protected void initChannel(SocketChannel socketChannel) throws Exception {
        	  
        	  ChannelPipeline pipeline = socketChannel.pipeline();   
             // pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8));    
             pipeline.addLast("frameEncoder", new LengthFieldPrepender(8));   
              
              //pipeline.addLast("encoder", new MyEncoder(CharsetUtil.UTF_8));  
               pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));    

              pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));    
              socketChannel.pipeline().addLast(nettyClientHandler); //6.往Pipeline链中添加自定义的handler
              
              
              
//              pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));    
//              pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));    
//              pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));    
//              pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));   
//            socketChannel.pipeline().addLast("decoder", new StringDecoder());
//            socketChannel.pipeline().addLast("encoder", new StringEncoder());
//            socketChannel.pipeline().addLast(nettyClientHandler);
          }
        });
    try {
      ChannelFuture future = bootstrap.connect("127.0.0.1", 8088).sync();
      System.out.println("客户端发送成功....");
      //发送消息
      future.channel().writeAndFlush(msg);
      // 等待连接被关闭
      future.channel().closeFuture().sync();
      return nettyClientHandler.getResponseResult();
    } catch (Exception e) {
    	e.printStackTrace();
    	 System.out.println("客户端Netty失败");
     
    } finally {
      //以一种优雅的方式进行线程退出
      group.shutdownGracefully();
    }
	return msg;
  }
  
  public static void main(String[] args) throws IOException {

		String reqMsg="<service><prcscd>pfms03</prcscd>\r\n" +
				"<servtp>HZF</servtp>\r\n" + 
				"<servsq>20200608000000000001</servsq>\r\n" + 
				"<userid>0170901</userid>\r\n" + 
				"<brchno>017001</brchno>\r\n" + 
				"<job_number>GH88888888</job_number>\r\n" +
				"<name>测试小陈</name>\r\n" +
				"<sex>男</sex>\r\n" +
              "<birthday>134</birthday>\r\n" +
              "<organ_dept_name>2341341</organ_dept_name>\r\n" +
              "<nation>1</nation>\r\n" +
              "<is_store>1</is_store>\r\n" +
              "<id_card>1</id_card>\r\n" +
              "<political_status>1</political_status>\r\n" +
              "<name_once_used>1</name_once_used>\r\n" +
              "<file_number>1</file_number>\r\n" +
              "<birth_place>1</birth_place>\r\n" +
              "<native_place>1</native_place>\r\n" +
              "<in_work_day>2020-03-11</in_work_day>\r\n" +
              "<is_league_member>1</is_league_member>\r\n" +
              "<in_member_day>2020-03-11</in_member_day>\r\n" +
              "<in_party_day>2020-03-11</in_party_day>\r\n" +
              "<health_condition>1</health_condition>\r\n" +
              "<full_education_degree>1</full_education_degree>\r\n" +
              "<full_colleges_majors>1</full_colleges_majors>\r\n" +
              "<part_education_degree>1</part_education_degree>\r\n" +
              "<part_colleges_majors>1</part_colleges_majors>\r\n" +
              "<now_post>1</now_post>\r\n" +
              "<establishment_organization>1</establishment_organization>\r\n" +
              "<secondary_organization>21</secondary_organization>\r\n" +
              "<station>1</station>\r\n" +
              "<post>1</post>\r\n" +
              "<line>1</line>\r\n" +
              "<nature_employment>1</nature_employment>\r\n" +
              "<rank>1</rank>\r\n" +
              "<person_state>1</person_state>\r\n" +
              "<is_manager>1</is_manager>\r\n" +

              "<jobresume>\n" +
              "<record>\n" +
              "<begin_time>2020-03-11</begin_time>\r\n" +
              "<end_time>2020-03-11</end_time>\r\n" +
              "<post>职务</post>\r\n" +
              "<operate>edit</operate>\r\n" +
              "<company>112</company>\r\n" +
              "</record>\n" +
              "<record>\n" +
              "<begin_time>2020-03-11</begin_time>\r\n" +
              "<end_time>2020-03-11</end_time>\r\n" +
              "<post>职务</post>\r\n" +
              "<operate>edit</operate>\r\n" +
              "<company>112</company>\r\n" +
              "</record>\n" +

              "</jobresume>\r\n"+

              "<education>\n" +
              "<record>\n" +
              "<in_school_day>2020-03-11</in_school_day>\r\n" +
              "<school_name>2020-03-11</school_name>\r\n" +
              "<major_name>2020-03-11</major_name>\r\n" +
              "<education_level>本科</education_level>\r\n" +
              "</record>\n" +
              "<record>\n" +
              "<in_school_day>2020-03-11</in_school_day>\r\n" +
              "<school_name>2020-03-11</school_name>\r\n" +
              "<major_name>2020-03-11</major_name>\r\n" +
              "<education_level>本科</education_level>\r\n" +
              "</record>\n" +
              "</education>\r\n"+

				"</service>";
		
		
		// reqMsg="<service><prcscd>pfms03</prcscd>\r\n";
		System.out.println(reqMsg.getBytes("utf-8").length);

		
		String s= NettyClientUtil.helloNetty(reqMsg);
		System.out.println(s);
	    }


}